package gupao.concurrency.threads;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CountDownLatch;

@Slf4j
public class NewThreadDemo {

    public static void main(String[] args) throws InterruptedException {
        testThread();
    }

    private static void testThread() throws InterruptedException {
        CountDownLatch l = new CountDownLatch(4);
        //1
        new Thread() {
            @Override
            public void run() {
                Thread t = Thread.currentThread();
                t.setName("setNameTest");
                log.info("Without target and name: [self] run in {}, group: {}, daemon: {}, Priority: {}",
                        t.getName(), t.getThreadGroup().getName(), t.isDaemon(), t.getPriority());
                l.countDown();
            }
        }.start();
        //2
        new Thread(() -> {
            Thread t = Thread.currentThread();
            log.info("Without name: [runnable] run in {}, group: {}, daemon: {}",
                    t.getName(), t.getThreadGroup().getName(), t.isDaemon());
            l.countDown();
        }).start();

        //3
        String name1 = "test-name-1";
        new Thread(() -> {
            Thread t = Thread.currentThread();
            log.info("With name={}: [runnable] run in {}, group: {}, daemon: {}",
                    name1, t.getName(), t.getThreadGroup().getName(), t.isDaemon());
            l.countDown();
        }, name1).start();

        //4
        String name2 = "test-name-2";
        ThreadGroup threadGroup = new ThreadGroup("group_1");
        new Thread(threadGroup,
                () -> {
                    Thread t = Thread.currentThread();
//                    t.setDaemon(true);
                    log.info("With name {}: [runnable] run in {}, group: {}, daemon: {}",
                            name2, t.getName(), t.getThreadGroup().getName(), t.isDaemon());
                    l.countDown();
                }, name2).start();

        l.await();

    }
}
